﻿@{
    ViewBag.Title = "用户管理";
    Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/_AdminLayout.cshtml";
}

<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
    <ul class="layui-tab-title">
        <li class="layui-this">用户管理</li>
    </ul>
</div>
<div class="demoTable">
    关键字：
  <div class="layui-inline">
      <input class="layui-input" name="id" id="txtKey" autocomplete="off">
  </div>
    <button class="layui-btn" data-type="reload">搜索</button>
    <button class="layui-btn" data-type="add">添加</button>
</div>
<table id="demo" lay-filter="test"></table>

@section scripts{

    <script>
        layui.config({
            base: "~/Content/res/mods/"
        }).use(['layer', 'table'], function () {
            var layer = layui.layer
                , table = layui.table
                , $ = layui.jquery
                , form = layui.form

            table.render({
                elem: '#demo'
                , height: 'full-200'
                , url: '@Url.Action("QueryUserInfoList", "AdminHome")'
                , cellMinWidth: 80
                , id: 'userTable'
                , even: true
                , cols: [[
                    { field: 'userid', title: 'id', sort: true, fixed: 'left' }
                    , { field: 'username', title: '用户名', sort: true }
                    , { field: 'userpwd', title: '密码', sort: true }
                    , { field: 'email', title: '邮箱' }
                    , { field: 'state', title: '状态', width:100, templet: '#switchState', unresize: true }
                    , { fixed: 'right', title: '操作', width: 178, align: 'center', toolbar: '#barDemo' }
                ]]
                , text: {
                    none: '暂无相关数据'
                }
                , page: true
            });

            //监听性别操作
            form.on('switch(stateDemo)', function (obj) {
                layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
            });

            active = {
                reload: function () {
                    var keyName = $('#txtKey');
                    //执行重载
                    table.reload('userTable', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                      , where: {
                          key: {
                              id: keyName.val()
                          }
                      }
                    });
                },
                add: function () {
                    layer.open({
                        type: 2
                        , title: '用户添加'
                        , scrollbar: false
                        , content: '@Url.Action("UserAdd")'
                        , area: ['400px', '450px']
                        , shadeClose: true
                        , maxmin: true
                    })
                }
            };

            table.on('tool(test)', function (obj) {
                var itemData = obj.data;
                if (obj.event === 'detail') {
                    layer.msg('ID：' + itemData.userid + ' 的查看操作');
                } else if (obj.event === 'del') {
                    layer.confirm('真的删除id为' + itemData.userid + '行么', function (index) {
                        $.post("@Url.Action("UserDel")", { id: itemData.userid }, function (data) {
                            if (data.code == 0) {
                                obj.del();
                                layer.close(itemData.userid);
                                layer.msg(data.msg);
                                active.reload();
                            }
                            else
                            {
                                layer.msg(data.msg);
                            }
                        }, "json");
                    });
                } else if (obj.event === 'edit') {
                    layer.open({
                        type: 2
                        , title: '用户修改'
                        , scrollbar: false
                        , content: '@Url.Action("UserEdit")/' + itemData.userid
                        , area: ['400px', '450px']
                        , shadeClose: true
                        , maxmin: true

                    })
                    //layer.alert('编辑行：<br>' + JSON.stringify(data))
                } 
            })

            $('.demoTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });


        });
    </script>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="switchState">
      <!-- 这里的 checked 的状态只是演示 -->
        @{
            int delState = (int)Bottle.CommunityForum.Model.Enum.UserEnum.StateNormal;
          }
      <input type="checkbox" name="state" value="{{d.state}}" lay-skin="switch" lay-text="正常|禁用" lay-filter="stateDemo" {{ d.state == @delState ? 'checked' : '' }}>
    </script>
}
